$(function() {
	// 初始化列表
	$('table.dic-list').list({
		header : false,
		footer : false,
		search : false,
		checkbox : true,
	});

	// 添加
	$('.dic-add').on('click', function() {
		var dicCode = $('input#dicCode').val();
		var dicVal = $('input#dicValue').val();

		if (!dicVal) {
			alert('请正确填写字典值信息！');
			return false;
		}

		$.ajax({
			url : $.suredy.contextPath + '/config/dic/add',
			type : 'post',
			traditional : true,
			data : {
				dicCode : dicCode,
				dicValue : dicVal
			},
			success : function(success, textStatus, jqXHR) {
				if (!success) {
					alert('添加操作失败！');
					return false;
				}

				var listUri = $.suredy.contextPath + '/config/dic/list/' + dicCode;

				$.suredy.loadContent(listUri);
			},
			error : function(jqXHR, textStatus, errorThrown) {
				alert('添加操作失败！\n' + errorThrown);
			}
		});
	});

	// 删除
	$('.dic-delete').on('click', function() {
		var checked = $.suredy.list.checked($('table.dic-list'));

		if (checked.length === 0) {
			return false;
		}

		if (!window.confirm('确定删除[' + checked.length + ']条字典信息么？'))
			return false;

		var id = [];
		checked.each(function(i) {
			id[i] = $(this).data('id');
		});
		
		var dicCode = $('input#dicCode').val();

		$.ajax({
			url : $.suredy.contextPath + '/config/dic/delete',
			type : 'post',
			traditional : true,
			data : {
				id : id
			},
			success : function(success, textStatus, jqXHR) {
				if (!success) {
					alert('删除操作失败！');
					return false;
				}

				var listUri = $.suredy.contextPath + '/config/dic/list/' + dicCode;

				$.suredy.loadContent(listUri);
			},
			error : function(jqXHR, textStatus, errorThrown) {
				alert('删除操作失败！\n' + errorThrown);
			}
		});
	});
});